from fastapi import APIRouter, Depends, Query
from pydantic import Field
from sqlalchemy import text
from sqlalchemy.orm import Session

from commons.BaseResponse import BaseResponse
from commons.PaginationBase import PaginationBase
from datebase import get_db
from utils.EncryptionUtil import oauth2_scheme

data_slog_router = APIRouter(prefix="/sLog", tags=["sLog"], dependencies=[Depends(oauth2_scheme)])


class SLogSearch(PaginationBase):
    sort: str = Field(default=None, description="排序字段")
    order: str = Field(default="desc", description="排序方式")
    ftype: int = Field(default=None, description="类型")
    fmacno: str = Field(default=None, description="设备编号")
    faction: int = Field(default=None, description="操作id")
    fsceneno: int = Field(default=None, description="场景id")
    foperator: int = Field(default=None, description="操作人id")
    fcreatetime: str = Field(default=None, description="创建时间")


@data_slog_router.get("/list")
def get_slog_list(slog: SLogSearch = Query(), db: Session = Depends(get_db)):
    sql = "select count(1) from s_log where 1=1 "
    param = {}
    if slog.ftype is not None:
        sql = sql + " and ftype= :ftype"
        param["ftype"] = slog.ftype
    if slog.fmacno is not None:
        sql = sql + " and fmacno= :fmacno"
        param["fmacno"] = slog.fmacno
    if slog.faction is not None:
        sql = sql + " and faction= :faction"
        param["faction"] = slog.faction
    if slog.fsceneno is not None:
        sql = sql + " and fsceneno= :fsceneno"
        param["fsceneno"] = slog.fsceneno
    if slog.foperator is not None:
        sql = sql + " and foperator= :foperator"
        param["foperator"] = slog.foperator
    if slog.fcreatetime is not None:
        sql = sql + " and fcreatetime= :fcreatetime"
        param["fcreatetime"] = slog.fcreatetime

    count = db.execute(text(sql), param).scalar()

    sql = "select faction,fcreatetime,finstructions,fmacno,fno,foperator,fsceneno,ftype,fpoint_no"
    sql = sql + " from s_log where 1=1"
    param = {}
    if slog.ftype is not None:
        sql = sql + " and ftype= :ftype"
        param["ftype"] = slog.ftype
    if slog.fmacno is not None:
        sql = sql + " and fmacno= :fmacno"
        param["fmacno"] = slog.fmacno
    if slog.faction is not None:
        sql = sql + " and faction= :faction"
        param["faction"] = slog.faction
    if slog.fsceneno is not None:
        sql = sql + " and fsceneno= :fsceneno"
        param["fsceneno"] = slog.fsceneno
    if slog.foperator is not None:
        sql = sql + " and foperator= :foperator"
        param["foperator"] = slog.foperator
    if slog.fcreatetime is not None:
        sql = sql + " and fcreatetime= :fcreatetime"
        param["fcreatetime"] = slog.fcreatetime
    if slog.sort is not None:
        sql = sql + " and sort=:sort"
        param["sort"] = slog.sort
    sql = sql + " limit :limit offset :offset"
    param["offset"] = slog.offset
    param["limit"] = slog.limit

    data = db.execute(text(sql), param).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['faction'] = data[key][0]
        d1['fcreatetime'] = data[key][1]
        d1['finstructions'] = data[key][2]
        d1['fmacno'] = data[key][3]
        d1['fno'] = data[key][4]
        d1['foperator'] = data[key][5]
        d1['fpointNo'] = data[key][8]
        d1['fsceneno'] = data[key][6]
        d1['ftype'] = data[key][7]
        d1['methodName'] = None
        data_list.append(d1)

    info_dict = {"list": data_list, "limit": slog.limit, "page": slog.pageIndex,
                 "pages": (count + slog.limit - 1) // slog.limit,
                 "total": count}
    return BaseResponse.success(data=info_dict)
